﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using IDAL;
using DALFactory;
using BLL.EmailService;
using System.Security.Cryptography;
using DataMode;


namespace BLL
{
    public class UserinfoBLL : IBLL.UserinfoIBLL
    {
        UserinfoIDAL userinfoIDAL = DALFactory.DataAccess.getUserinfoIDAL();


        public IQueryable getUserinfo(String username)
        {
            return userinfoIDAL.selectUser(username);
        }

        public void userRegister(String username, String password, string email, string sex, DateTime birthday, string safe_Q, string safe_A)
        {
            userinfoIDAL.addUser(username, GetMD5String(password), email, sex, birthday, safe_Q, safe_A);
            EmailClient emailClient = new EmailClient();
            emailClient.SendEmail(email, "Post 90s Museum验证邮件", "非常感谢您注册Post 90s Museum。<a href='http://localhost:12442/login_and_signup/signup_succ.aspx?user=" + username + "&&token=" + GetMD5String(username + "museum") + "'>请点击验证邮件");
        }

        public static string GetMD5String(string str)
        {
            MD5 md5 = MD5.Create();
            byte[] b = Encoding.UTF8.GetBytes(str);
            byte[] md5b = md5.ComputeHash(b);
            md5.Clear();
            StringBuilder sb = new StringBuilder();
            foreach (var item in md5b)
            {
                sb.Append(item.ToString("x2"));
            }
            return sb.ToString();
        }

        public bool userValidate(string username,string token)
        {
            if (GetMD5String(username + "museum").Equals(token))
            {
                userinfoIDAL.modify(username,"已验证");
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool login(string username, string password)
        {
            IQueryable<userinfo> user = (IQueryable<userinfo>)userinfoIDAL.selectUser(username);
            if (user != null)
            {
                foreach (userinfo u in user)
                {
                    if (u.password.Equals(password)&&u.register_state.Equals("已验证"))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool hasUsername(string username)
        {
            IQueryable<userinfo> user = (IQueryable<userinfo>)userinfoIDAL.selectUser(username);
            int count = 0;
            if (user != null)
            {
                foreach (userinfo u in user)
                {
                    count++;
                }
            }
            if (count != 0) return true;
            return false;
        }

        public DataMode.userinfo getuserinfo(string name)
        {

            return userinfoIDAL.getuserinfo(name);
        }

        public void UpdateUserInfo(DataMode.userinfo ui, string name)
        {
            userinfoIDAL.UpdateUserInfo(ui, name);
            return;
        }
    }

}
